package org.elephas.webapp.frontend.component;

import java.util.Iterator;

import org.apache.wicket.authorization.Action;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.markup.repeater.util.ModelIteratorAdapter;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.Strings;
import org.elephas.model.BlogRole;
import org.elephas.model.Comment;
import org.elephas.model.Post;
import org.elephas.webapp.application.security.AuthorizeAction;
import org.elephas.webapp.frontend.component.common.TextileLabel;
import org.elephas.webapp.model.DetachableCommentModel;
import org.elephas.webapp.model.DetachablePostModel;

/**
 * Renders a post and its comments.
 *
 * @author Gerolf Seitz
 *
 */
public class PostPanel extends Panel {
	private static final long serialVersionUID = 1L;

	public PostPanel(final String id, final DetachablePostModel postModel) {
		super(id, new CompoundPropertyModel(postModel));
		add(new Label("title"));
		add(DateLabel.forDateStyle("created", "F-"));
		add(new TextileLabel("content"));
		add(new CategoriesLabel("categories"));
		add(new Label("user"));

		add(new EditLink("editLink", postModel));

		add(new CommentFormPanel("commentForm", postModel));

		add(new RefreshingView("comments") {
			private static final long serialVersionUID = 1L;

			@Override
			protected Iterator getItemModels() {
				return new ModelIteratorAdapter(postModel.getObject()
						.getComments().iterator()) {

					@Override
					protected IModel model(Object object) {
						return new CompoundPropertyModel(
								new DetachableCommentModel((Comment) object));
					}
				};
			}

			@Override
			protected void populateItem(final Item item) {
				Comment comment = (Comment) item.getModelObject();
				item.setOutputMarkupId(true);

				item.add(new NameLink("url", new Model(comment.getUrl()),
						new Model(comment.getName())));
				item.add(new MultiLineLabel("content"));
				item.add(DateLabel.forDateStyle("created", "FS"));

				item.add(new Link("deleteComment", item.getModel()) {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						Comment comment = (Comment) getModelObject();
						Post post = comment.getPost();
						post.deleteCommend(comment);
					}
				});
			}
		});
	}

	private final class NameLink extends ExternalLink {
		private static final long serialVersionUID = 1L;

		private NameLink(String id, IModel href, IModel label) {
			super(id, href, label);
		}

		@Override
		protected void onComponentTag(ComponentTag tag) {
			super.onComponentTag(tag);
			if (Strings.isEmpty(getModelObjectAsString())) {
				tag.setName("span");
			}
		}
	}

	@AuthorizeAction(role = BlogRole.AUTHOR, action = Action.RENDER)
	private final class EditLink extends Link {
		private final DetachablePostModel postModel;
		private static final long serialVersionUID = 1L;

		private EditLink(String id, DetachablePostModel postModel) {
			super(id);
			this.postModel = postModel;
		}

		@Override
		public void onClick() {
			PostPanel panel = PostPanel.this;
			panel.replaceWith(new EditPostPanel(PostPanel.this.getId(),
					postModel));
		}
	}

}
